﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using ihongma.Common;
using System.Text;
using System.Data;
using System.Drawing;
using System.IO;


namespace ihongma.Web.tools
{
    /// <summary>
    /// pic 的摘要说明
    /// </summary>
    public class pic : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int pageIndex = DTRequest.GetQueryInt("page");
            int sort = DTRequest.GetQueryInt("sort");
           string strWhere=sort!=0?"category_id="+sort.ToString():"";
          
           int recordCount=0;
           context.Response.Write(CreateJsonParameters(new BLL.photo().GetList(15, pageIndex, strWhere, "id desc", out recordCount).Tables[0], recordCount));
            
        }
        public static string height(string path)
        {
            if (!string.IsNullOrEmpty(path))
            {
                string str=HttpContext.Current.Server.MapPath(path);
                if (File.Exists(str))
                {
                    Image pic = Image.FromFile(str);//strFilePath是该图片的绝对路径
                    using (System.Drawing.Image bmp = new System.Drawing.Bitmap(pic))
                    {
                        return bmp.Height.ToString()+"@"+bmp.Width.ToString();//高度像素值
                    }
                }
                else
                {
                    return "100@100";
                }
            }
            else
            {
                return "100@100";
            }
        }
        public static string CreateJsonParameters(DataTable dt, int recordCount)
        {
            string column = "id,title,img_url,market_price,sell_price";
            StringBuilder JsonString = new StringBuilder();
            //Exception Handling        
            if (dt != null && dt.Rows.Count > 0)
            {
                JsonString.Append("{");
                JsonString.Append("\"pic\":[ ");
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    JsonString.Append("{ ");
                    for (int j = 0; j < dt.Columns.Count; j++)
                    {
                        foreach (string str in column.Split(','))
                        {

                            if (dt.Columns[j].ColumnName == str)
                            {

                                if (dt.Columns[j].ColumnName == "title" || dt.Columns[j].ColumnName == "id")
                                {
                                    JsonString.Append("\"" + dt.Columns[j].ColumnName.ToString() + "\":" + "\"" +Utils.DropHTML(dt.Rows[i][j].ToString()) + "\",");
                                }

                                else if (dt.Columns[j].ColumnName == "market_price")
                                {
                                    JsonString.Append("\"Height\":" + int.Parse(dt.Rows[i][j].ToString()) + ",");
                                   
                                   
                                    
                                }
                                else if (dt.Columns[j].ColumnName == "sell_price")
                                {
                                    JsonString.Append("\"width\":" + int.Parse(dt.Rows[i][j].ToString()) + ",");
                                }
                                else
                                {
                                    JsonString.Append("\"" + dt.Columns[j].ColumnName.ToString() + "\":" + "\"" + dt.Rows[i][j].ToString() + "\"");
                                }
                            }

                        }
                    }
                    /**/
                    /**/
                    /**/
                    /*end Of String*/
                    if (i == dt.Rows.Count - 1)
                    {
                        JsonString.Append("} ");
                    }
                    else
                    {
                        JsonString.Append("}, ");
                    }
                }
                JsonString.Append("],\"recordCount\":" + recordCount.ToString() + "");
                JsonString.Append("}");
                return JsonString.ToString();
            }
            else
            {
                return null;
            }
        }

     
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}